const { query } = require('koa/lib/request');

const Controller = require('egg').Controller;
class Clazz extends Controller {
    async index(){
        this.ctx.body = await this.app.model.Clazz.findAll();
    }
    async update(){
        let id = this.ctx.params.id;
        let name=this.ctx.request.body.name
        await this.ctx.model.Clazz.update(
            {name:name},
            {where:{id:id}},
        )
        this.ctx.body = await this.app.model.Clazz.findAll();
    }
    async create(){
        let name=this.ctx.request.body.name;
        try{
            await this.app.model.Clazz.create({name:name})
            let data = await this.app.model.Clazz.findAll();
            this.ctx.body = data;
        }catch{
            this.ctx.body='错误'
        }
    }
    async destroy(){
        let id = this.ctx.params.id;
        await this.ctx.app.model.Clazz.destroy(
            {where:{id:id}}
        )
        // this.ctx.body 
        let p= await this.ctx.app.model.Clazz.findAll()
        let ifa= await this.app.middleware.ifk(p)
        console.log(p);
        if(ifa){
            this.ctx.body=p
        }else{
            this.ctx.body={
                code:'500',
                msg:'错误'
            }
        }
    }








    async show(){
        let id=this.ctx.params.id;
        let data=await this.app.model.Clazz.findAll({
            where:{id:id},
        });
        if(data.length){
            this.ctx.body = data
        }else{
            this.ctx.body = {
                code:'200',
                msg:'无数据'
            }
        }
        
    }
    async new(){

    }
}

module.exports = Clazz;